<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsLogSummary extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['gl.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $goods_id = $this->request->post('goods_id');
            if(!empty($goods_id)){
                $where['gl.goods_id'] = ['in',$goods_id];
            }
            $stores_id = $this->request->post('stores_id');
            if(!empty($stores_id)){
                $where['gl.stores_id'] = ['in',$stores_id];
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_goods_log')->alias('gl')
                    ->join('kuyou_goods g','g.id=gl.goods_id')
                    ->join('kuyou_stores s','s.id=gl.stores_id')
                    ->where($where)
                    ->page($page,$limit)
                    ->group('gl.goods_id,gl.stores_id')
                    ->field('g.goods_code,g.goods_name,g.goods_type_id,g.goods_barcode,g.goods_image,g.goods_specification_id,g.note,g.unit_id,s.name as store_name,
                gl.goods_id,gl.stores_id')
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_log')->alias('gl')
                    ->join('kuyou_goods g','g.id=gl.goods_id')
                    ->join('kuyou_stores s','s.id=gl.stores_id')
                    ->where($where)
                    ->group('gl.goods_id,gl.stores_id')
                    ->field('g.goods_code,g.goods_name,g.goods_type_id,g.goods_barcode,g.goods_image,g.goods_specification_id,g.note,g.unit_id,s.name as store_name,
                gl.goods_id,gl.stores_id')
                    ->select();
            }
            $row_count = Db::name('kuyou_goods_log')->alias('gl')
                ->join('kuyou_goods g','g.id=gl.goods_id')
                ->join('kuyou_stores s','s.id=gl.stores_id')
                ->where($where)->group('gl.goods_id,gl.stores_id')->count();

            foreach ($list as $list_k=>$list_v){
                $list[$list_k]['type_name'] = Db::name('kuyou_goods_type')->where(['id'=>$list_v['goods_type_id']])->value('supplier_type_name');
                //期初
                $enter_basics = 0;$enter_cost = 0;
                $period = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'1'])
                    ->field('sum(balance_basics) as basics,sum(balance_cost) as cost')
                    ->find();
                $list[$list_k]['period_basics'] = $period['basics'];
                $list[$list_k]['period_cost'] = $period['cost'];
                //调拨入库
                $allocation_e = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'6'])
                    ->field('sum(enter_basics) as basics,sum(enter_cost) as cost')
                    ->find();
                $enter_basics+=(float)$allocation_e['basics'];
                $enter_cost+=(float)$allocation_e['cost'];
                $list[$list_k]['allocation_e_basics'] = $allocation_e['basics'];
                $list[$list_k]['allocation_e_cost'] = $allocation_e['cost'];
                //普通采购
                $buy_o = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'2'])
                    
                    ->field('sum(enter_basics) as basics,sum(enter_cost) as cost')
                    ->find();
                $enter_basics+=(float)$buy_o['basics'];
                $enter_cost+=(float)$buy_o['cost'];
                $list[$list_k]['buy_o_basics'] = $buy_o['basics'];
                $list[$list_k]['buy_o_cost'] = $buy_o['cost'];
                //销售退回
                $sale_e = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'5'])
                    
                    ->field('sum(enter_basics) as basics,sum(enter_cost) as cost')
                    ->find();
                $enter_basics+=(float)$sale_e['basics'];
                $enter_cost+=(float)$sale_e['cost'];
                $list[$list_k]['sale_e_basics'] = $sale_e['basics'];
                $list[$list_k]['sale_e_cost'] = $sale_e['cost'];

                //盘盈
                $surplus = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'8'])
                    
                    ->field('sum(enter_basics) as basics,sum(enter_cost) as cost')
                    ->find();
                $enter_basics+=(float)$surplus['basics'];
                $enter_cost+=(float)$surplus['cost'];
                $list[$list_k]['surplus_basics'] = $surplus['basics'];
                $list[$list_k]['surplus_cost'] = $surplus['cost'];

                //其他入库
                $other_e = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'9'])
                    
                    ->field('sum(enter_basics) as basics,sum(enter_cost) as cost')
                    ->find();
                $enter_basics+=(float)$other_e['basics'];
                $enter_cost+=(float)$other_e['cost'];
                $list[$list_k]['other_e_basics'] = $other_e['basics'];
                $list[$list_k]['other_e_cost'] = $other_e['cost'];

                $list[$list_k]['enter_basics'] = $enter_basics;
                $list[$list_k]['enter_cost'] = $enter_cost;

                $out_basics = 0;$out_cost = 0;
                //调拨出库
                $allocation_o = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'6'])
                    
                    ->field('sum(out_basics) as basics,sum(out_cost) as cost')
                    ->find();
                $out_basics+=(float)$allocation_o['basics'];
                $out_cost+=(float)$allocation_o['cost'];
                $list[$list_k]['allocation_o_basics'] = $allocation_o['basics'];
                $list[$list_k]['allocation_o_cost'] = $allocation_o['cost'];
                //普通销售
                $sale_o = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'4'])
                    
                    ->field('sum(out_basics) as basics,sum(out_cost) as cost')
                    ->find();
                $out_basics+=(float)$sale_o['basics'];
                $out_cost+=(float)$sale_o['cost'];
                $list[$list_k]['sale_o_basics'] = $sale_o['basics'];
                $list[$list_k]['sale_o_cost'] = $sale_o['cost'];
                //采购退回
                $buy_e = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'3'])
                    
                    ->field('sum(out_basics) as basics,sum(out_cost) as cost')
                    ->find();
                $out_basics+=(float)$buy_e['basics'];
                $out_cost+=(float)$buy_e['cost'];
                $list[$list_k]['buy_e_basics'] = $buy_e['basics'];
                $list[$list_k]['buy_e_cost'] = $buy_e['cost'];

                //盘亏
                $loss = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'10'])
                    
                    ->field('sum(out_basics) as basics,sum(out_cost) as cost')
                    ->find();
                $out_basics+=(float)$loss['basics'];
                $out_cost+=(float)$loss['cost'];
                $list[$list_k]['loss_basics'] = $loss['basics'];
                $list[$list_k]['loss_cost'] = $loss['cost'];

                //其他出库
                $other_o = Db::name('kuyou_goods_log')
                    ->where(['goods_id'=>$list_v['goods_id'],'stores_id'=>$list_v['stores_id'],'business_type'=>'11'])
                    
                    ->field('sum(out_basics) as basics,sum(out_cost) as cost')
                    ->find();
                $out_basics+=(float)$other_o['basics'];
                $out_cost+=(float)$other_o['cost'];
                $list[$list_k]['other_o_basics'] = $other_o['basics'];
                $list[$list_k]['other_o_cost'] = $other_o['cost'];

                $list[$list_k]['out_basics'] = $out_basics;
                $list[$list_k]['out_cost'] = $out_cost;

                $list[$list_k]['balance_basics'] = (float)$period['basics']+$enter_basics-$out_basics;
                $list[$list_k]['balance_cost'] = (float)$period['cost']+$enter_cost-$out_cost;

                $list[$list_k]['unit_name'] = Db::name('kuyou_unit')->where(['id'=>$list_v['unit_id']])->value('name');
                $store = Db::name('kuyou_stores')->where(['id'=>$list_v['stores_id']])->field('store_code,name')->find();
                $list[$list_k]['store_code'] = $store['store_code'];
                $list[$list_k]['store_name'] = $store['name'];
            }

            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal  = $setList['price_decimal']['value'];

            $format = [
                'period_basics'  =>$number_decimal,
                'period_cost'  =>$price_decimal,
                'allocation_e_basics'    =>$number_decimal,
                'allocation_e_cost'    =>$price_decimal,
                'buy_o_basics'    =>$number_decimal,
                'buy_o_cost'    =>$price_decimal,
                'sale_e_basics'      =>$number_decimal,
                'sale_e_cost'      =>$price_decimal,
                'enter_basics'      =>$number_decimal,
                'enter_cost'      =>$price_decimal,

                'allocation_o_basics'      =>$number_decimal,
                'allocation_o_cost'      =>$price_decimal,
                'sale_o_basics'      =>$number_decimal,
                'sale_o_cost'      =>$price_decimal,
                'buy_e_basics'      =>$number_decimal,
                'buy_e_cost'      =>$price_decimal,
                'out_basics'      =>$number_decimal,
                'out_cost'      =>$price_decimal,
                'balance_basics'      =>$number_decimal,
                'balance_cost'      =>$price_decimal,
            ];
            $count = [];
            foreach ($list as $list_k=>$list_v){
                //格式化数字输出
                foreach ($format as $k=>$v){
                    if(isset($list_v[$k]) && $list_v[$k]!==null){
                        $list[$list_k][$k]  = round($list_v[$k],$v);
                        if(isset($count[$k])){
                            $count[$k] += $list_v[$k];
                        }else{
                            $count[$k] = $list_v[$k];
                        }
                    }else{
                        if(isset($count[$k])){
                            $count[$k] += 0;
                        }else{
                            $count[$k] = 0;
                        }
                    }
                }
            }
            foreach ($count as $k=>$v){
                if(isset($format[$k])){
                    $count[$k]  = round($v,$format[$k]);
                }
            }

            $result = array("count" => $row_count, "list" => $list,'sum'=>$count);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
